#include "ship.h"

namespace sprites {
	Ship::Ship(Vector3D _pos, MatterType _type, float boundRadius)
		: Sprite(_pos, _type, boundRadius, 1.0f), bullet(0), maxLeft(-15), maxRight(15)
	{
		bulletShot = false;
		moveRSpeed= Vector3D(1.0f, 0.0f, 0.0f);
		build();
	}
	Ship::Ship(Vector3D _pos, MatterType _type, float boundRadius, float scale)
		: Sprite(_pos, _type, boundRadius, scale), bullet(0), maxLeft(-15), maxRight(15)
	{
		bulletShot = false;
		moveRSpeed= Vector3D(1.0f, 0.0f, 0.0f);
		build();
	}
	Ship::~Ship() {
	  root->destroy();
	  if(bullet!=0)
		  bullet->~Bullet();
	}

	void Ship::build(){
		Cube* shipBody = new Cube(5.5f, 2.0f, 1.0f, 0xFF000100);
		root->setShape(shipBody);
		root->setTranslation(0.0f, -0.5f, 0.0f);

		ParentNode* bodyTop = new ParentNode();
		root->addChild(bodyTop);
		buildBodyTop(bodyTop);
		bodyTop->setTranslation(0.0f, 1.25f, 0.0f);

		ParentNode* cannon = new ParentNode();
		root->addChild(cannon);
		buildCannon(cannon);
		cannon->setTranslation(0.0f, 1.75f, 0.0f);

		ParentNode* turret = new ParentNode();
		root->addChild(turret);
		buildTurret(turret);
		turret->setTranslation(0.0f, 2.5f, 0.0f);
	}

	void Ship::buildBodyTop(ParentNode* _parent){
		Cube* bodyTop = new Cube(4.5f, 0.5f, 1.0f, 0xFF000100);
		_parent->addChild(bodyTop);
	}

	void Ship::buildCannon(ParentNode* _parent){
		Cube* cannon = new Cube(1.5f, 1.5f, 1.0f, 0xFF000100);
		_parent->addChild(cannon);
	}

	void Ship::buildTurret(ParentNode* _parent){
		Cube* turret = new Cube(0.5f, 1.0f, 1.0f, 0xFF000100);
		_parent->addChild(turret);
	}

	//collision part
	BindingSphere Ship::getBindingSphere()
	{
		bound.moveTo(parent.getTranslation());
		return bound;
	}


	std::vector<BindingSphere> Ship::getBindingShapes()
	{
		bound.moveTo(parent.getTranslation());
		bindingShapes = vector<BindingSphere>();
		bindingShapes.push_back(bound);
		return bindingShapes;
	}
	
	void Ship::handleCollision(MatterType oType, BindingSphere sphere)
	{
		switch(oType)
		{
		case collisions::BARRIER:
			break;
		case collisions::SHIPBULLET :
			break;
		case collisions::INVADERBULLET :
			dead = true;
			break;
		case collisions::INVADER :
			dead = true;
			break;
		case collisions::SHIP :
			break;
		}
	}

	//controls for the ship
	void Ship::moveLeft()
	{
		if(parent.getTranslation().X > maxLeft)
			parent-=moveRSpeed;
	}
	void Ship::moveRight()
	{
		if(parent.getTranslation().X < maxRight)
			parent+=moveRSpeed;
	}

	//returns a pointer to the bullet
	Bullet* Ship::shoot()
	{
		if(!bulletShot)
		{
			if(bullet == 0)
			{
				bullet = new Bullet(parent.getTranslation(), collisions::SHIPBULLET, 0.05f, scale);
			}
			else
			{
				bullet->setPosition(parent.getTranslation());
				bullet->revive();
			}
			bulletShot = true;
		}
		return bullet;
	}

	//used by game to draw ship to screen
	void Ship::draw()
	{
		parent.draw();
		if(bulletShot && !bullet->isDead())
		{
			//do nothing
		}
		else
		{
			bulletShot = false;
		}
	}

	bool Ship::isShooting()
	{
		return bulletShot;
	}

	void Ship::reset()
	{
		//make sure we can fire bullets
		bulletShot = false;
		setPosition(pos);
		revive();
	}
}